<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mentalTest:testresult:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mentalTest:testresult:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mentalTest:testresult:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mentalTest:testresult:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="testresultList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="评估" align="center" prop="aiText" show-overflow-tooltip />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="强迫倾向" align="center" prop="ct" />
      <el-table-column label="偏执倾向" align="center" prop="pt" />
      <el-table-column label="敌对情绪" align="center" prop="hl" />
      <el-table-column label="人际关系敏感" align="center" prop="is" />
      <el-table-column label="抑郁倾向" align="center" prop="dt" />
      <el-table-column label="焦虑倾向" align="center" prop="at" />
      <el-table-column label="学习压力感" align="center" prop="as" />
      <el-table-column label="适应不良" align="center" prop="mj" />
      <el-table-column label="情绪波动" align="center" prop="ef" />
      <el-table-column label="心理不平衡" align="center" prop="pi" />
      <el-table-column label="开始时间" align="center" prop="startTime" />
      <el-table-column label="测试时长" align="center" prop="during" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mentalTest:testresult:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mentalTest:testresult:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改大学生心理测试结果对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="评估" prop="aiText">
          <el-input v-model="form.aiText" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="用户id" prop="userid">
          <el-input v-model="form.userid" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="强迫倾向" prop="CT">
          <el-input v-model="form.CT" placeholder="请输入强迫倾向" />
        </el-form-item>
        <el-form-item label="偏执倾向" prop="PT">
          <el-input v-model="form.PT" placeholder="请输入偏执倾向" />
        </el-form-item>
        <el-form-item label="敌对情绪" prop="HL">
          <el-input v-model="form.HL" placeholder="请输入敌对情绪" />
        </el-form-item>
        <el-form-item label="人际关系敏感" prop="IS">
          <el-input v-model="form.IS" placeholder="请输入人际关系敏感" />
        </el-form-item>
        <el-form-item label="抑郁倾向" prop="DT">
          <el-input v-model="form.DT" placeholder="请输入抑郁倾向" />
        </el-form-item>
        <el-form-item label="焦虑倾向" prop="AT">
          <el-input v-model="form.AT" placeholder="请输入焦虑倾向" />
        </el-form-item>
        <el-form-item label="学习压力感" prop="AS">
          <el-input v-model="form.AS" placeholder="请输入学习压力感" />
        </el-form-item>
        <el-form-item label="适应不良" prop="MJ">
          <el-input v-model="form.MJ" placeholder="请输入适应不良" />
        </el-form-item>
        <el-form-item label="情绪波动" prop="EF">
          <el-input v-model="form.EF" placeholder="请输入情绪波动" />
        </el-form-item>
        <el-form-item label="心理不平衡" prop="PI">
          <el-input v-model="form.PI" placeholder="请输入心理不平衡" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-input v-model="form.startTime" placeholder="请输入开始时间" />
        </el-form-item>
        <el-form-item label="测试时长" prop="during">
          <el-input v-model="form.during" placeholder="请输入测试时长" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTestresult, getTestresult, delTestresult, addTestresult, updateTestresult } from "@/api/mentalTest/testresult";

export default {
  name: "Testresult",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 大学生心理测试结果表格数据
      testresultList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        address: null,
        nickName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询大学生心理测试结果列表 */
    getList() {
      this.loading = true;
      listTestresult(this.queryParams).then(response => {
        this.testresultList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        aiText: null,
        address: null,
        nickName: null,
        userid: null,
        CT: null,
        PT: null,
        HL: null,
        IS: null,
        DT: null,
        AT: null,
        AS: null,
        MJ: null,
        EF: null,
        PI: null,
        startTime: null,
        during: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加大学生心理测试结果";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTestresult(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改大学生心理测试结果";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTestresult(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTestresult(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除大学生心理测试结果编号为"' + ids + '"的数据项？').then(function() {
        return delTestresult(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mentalTest/testresult/export', {
        ...this.queryParams
      }, `testresult_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
